﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using static WenSkin.Controls.WenDataTextBox;

namespace WenSkin.Controls
{
    public class WenDataGridViewEdit : WenControl
    {
        public WenDataGridViewEdit() : base()
        {
            Size = new System.Drawing.Size(400, 300);
            wenDataGridView = new WenDataGridView()
            {
                Dock = DockStyle.Fill,
                AllowUserToAddRows = false,
                AllowUserToDeleteRows = false,
                ReadOnly = true
            };
            wenDataTextBox = new WenDataTextBox()
            {
                Dock = DockStyle.Top,
                Height = 80,
            };
            this.Controls.Add(WenDataGridView);
            this.Controls.Add(WenDataTextBox);
        }

        #region 私有属性
        private readonly WenDataGridView wenDataGridView;
        private readonly WenDataTextBox wenDataTextBox;
        private WenDataGridViewColumnCollection columns;
        #endregion

        #region 公有属性

        public WenDataGridView WenDataGridView => wenDataGridView;
        public WenDataTextBox WenDataTextBox=>wenDataTextBox;

        [Category("Wen"), Description("增加列"), DefaultValue(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public WenDataGridViewColumnCollection Columns => columns ??= new WenDataGridViewColumnCollection(this, this.WenDataGridView);

        [Category("Wen"), Description("列头高度"), DefaultValue(false)]
        public int TopHeight { get => WenDataTextBox.Height; set => WenDataTextBox.Height = value; }

        #endregion


        #region 类

        public class WenDataGridViewColumn : DataGridViewColumn  
        {
            public WenDataGridViewColumn():base()
            {
            }

            public WenDataGridViewColumn(DataGridViewCell cellTemplate) : base(cellTemplate)
            {
            }

            [Category("Wen"), Description("是否增加编辑项"), DefaultValue(false)]
            public bool Edit { get; set; } = false;

            [Category("Wen"), Description("是否主键"), DefaultValue(false)]
            public bool Identity { get; set; } = false;
        }

        public class WenDataGridViewColumnCollection : DataGridViewColumnCollection
        {
            private WenDataGridViewEdit owner;
            public WenDataGridViewColumnCollection(WenDataGridViewEdit owner,WenDataGridView wenDataGridView) : base(wenDataGridView)
            {
                this.owner = owner;
            }

            public new WenDataGridViewColumn this[int index] => base[index] as WenDataGridViewColumn;
            public new WenDataGridViewColumn this[string str] => base[str] as WenDataGridViewColumn;
            public override int Add(DataGridViewColumn column)
            {
                AddTextBox(column);
                return base.Add(column);
            }
            public override int Add(string columnName, string headerText)
            {
                return base.Add(columnName, headerText);
            }
            public override void AddRange(params DataGridViewColumn[] dataGridViewColumns)
            {
                foreach (var item in dataGridViewColumns)
                {
                    AddTextBox(item);
                }
                base.AddRange(dataGridViewColumns);
            }
            public override void Clear()
            {
                owner.WenDataTextBox.Items.Clear();
                base.Clear();
            }

            private void AddTextBox(DataGridViewColumn column)
            {
                if (column is WenDataGridViewColumn wen && wen.Edit)
                {
                    owner.WenDataTextBox.Items.Add(new WenDataTextBoxColumnText()
                    {
                        TextLable = wen.HeaderText,
                        ColumnName = wen.DataPropertyName,
                        Identity = wen.Identity
                    });
                }
            }
        }
        #endregion
    }
}
